package cn.stylefeng.guns.modular.demo.controller;


import cn.stylefeng.guns.dao.IGoodsService;
import cn.stylefeng.guns.dao.ISaleStockService;
import cn.stylefeng.guns.dao.ISupplierService;
import cn.stylefeng.guns.dao.service.*;
import cn.stylefeng.guns.modular.demo.excel.domain.RealTimeInventoryExcelVO;
import cn.stylefeng.guns.modular.demo.excel.domain.asilikang.*;
import cn.stylefeng.guns.modular.demo.excel.listener.aslikang.RealTimeInventoryListener;
import cn.stylefeng.guns.modular.demo.excel.listener.aslikang.Year23SaleListener;
import cn.stylefeng.guns.modular.demo.excel.listener.peisong.DistributionDocDetailListener;
import cn.stylefeng.guns.modular.demo.excel.listener.peisong.DistributionDocListener;
import cn.stylefeng.guns.modular.demo.excel.listener.peisong.SellDocDetailListener;
import cn.stylefeng.guns.modular.demo.excel.listener.peisong.SellDocListener;
import cn.stylefeng.roses.kernel.rule.annotation.BusinessLog;
import cn.stylefeng.roses.kernel.rule.enums.ResBizTypeEnum;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author quspacedraon
 * @since 2024-01-25
 */
@RestController
@Slf4j
@ApiResource(name = "导入", resBizType = ResBizTypeEnum.BUSINESS, path = "/upload")
public class UploadController {
    @Resource
    private ISaleStockService saleStockService;
    @Resource
    private ISupplierService supplierService;
    @Resource
    private IGoodsService goodsService;
    @Resource
    private IDistributionDocService distributionDocService;
    @Resource
    private IDistributionDocDetailService distributionDocDetailService;
    @Resource
    private ISellDocService sellDocService;
    @Resource
    private ISellDocDetailService sellDocDetailService;
    @Resource
    private IImportViewService importViewService;


    @ResponseBody
    @PostResource(name = "销售数据导入", path = "/upload/sale/data", requiredLogin = false, requiredPermission = false)
    @ApiOperation("销售数据导入")
    @BusinessLog
    public ResponseData<String> upload(MultipartFile file) throws IOException {
        Double 加点钱 = 0.0D;

        ArrayList<String> errorList = Lists.newArrayList();
        EasyExcel.read(file.getInputStream(), Year23SaleExcelVO.class, new Year23SaleListener(saleStockService, supplierService, goodsService, errorList)).sheet().doRead();
        return new SuccessResponseData<>(String.join(",", errorList));
    }


    @ResponseBody
    @PostResource(name = "配送单据导入", path = "/upload/DistributionDoc", requiredLogin = false, requiredPermission = false)
    @ApiOperation("配送单据导入")
    @BusinessLog
    public ResponseData<String> DistributionDoc(MultipartFile file) throws IOException {

        ArrayList<String> errorList = Lists.newArrayList();
        EasyExcel.read(file.getInputStream(), DistributionDocExcelVO.class, new DistributionDocListener(distributionDocService, errorList)).sheet().doRead();
        return new SuccessResponseData<>(String.join(",", errorList));
    }

    @ResponseBody
    @PostResource(name = "配送单据明细导入", path = "/upload/DistributionDoc/detail", requiredLogin = false, requiredPermission = false)
    @ApiOperation("配送单据明细导入")
    @BusinessLog
    public ResponseData<String> DistributionDocDetail(MultipartFile file) throws IOException {

        ArrayList<String> errorList = Lists.newArrayList();
        EasyExcel.read(file.getInputStream(), DistributionDocDetailExcelVO.class, new DistributionDocDetailListener(distributionDocDetailService, errorList)).sheet().doRead();
        return new SuccessResponseData<>(String.join(",", errorList));
    }

    @ResponseBody
    @PostResource(name = "销售单据导入", path = "/upload/sellDoc", requiredLogin = false, requiredPermission = false)
    @ApiOperation("销售单据导入")
    @BusinessLog
    public ResponseData<String> sellDoc(MultipartFile file) throws IOException {
        ArrayList<String> errorList = Lists.newArrayList();
        EasyExcel.read(file.getInputStream(), SellDocExcelVO.class, new SellDocListener(sellDocService, errorList)).sheet().doRead();
        return new SuccessResponseData<>(String.join(",", errorList));
    }

    @ResponseBody
    @PostResource(name = "销售单据明细导入", path = "/upload/sellDoc/detail", requiredLogin = false, requiredPermission = false)
    @ApiOperation("销售单据明细导入")
    @BusinessLog
    public ResponseData<String> sellDocDetail(MultipartFile file) throws IOException {
        ArrayList<String> errorList = Lists.newArrayList();
        EasyExcel.read(file.getInputStream(), SellDocExcelDetailVO.class, new SellDocDetailListener(sellDocDetailService, errorList)).sheet().doRead();
        return new SuccessResponseData<>(String.join(",", errorList));
    }


    @ResponseBody
    @PostResource(name = "实时库存导入", path = "/upload/realTimeInventory", requiredLogin = false, requiredPermission = false)
    @ApiOperation("实时库存导入")
    @BusinessLog
    public ResponseData<String> realTimeInventoryUpload(MultipartFile file) throws IOException {
        ArrayList<String> errorList = Lists.newArrayList();
        EasyExcel.read(file.getInputStream(), RealTimeInventoryExcelVO.class, new RealTimeInventoryListener(importViewService, errorList)).sheet().doRead();
        return new SuccessResponseData<>(String.join(",", errorList));
    }


//    /**
//     * 示例方法
//     *
//     * @author fengshuonan
//     * @since 2021/1/24 10:59
//     */
//    @PostResource(name = "商品列表", path = "/list", requiredLogin = true, requiredPermission = true)
//    @ApiOperation("商品列表")
//    public ResponseData<PageResult<Goods>> list(@RequestBody GoodsQueryRequest queryRuleRequest) {
//
//        Page<Goods> page = new Page<>();
//        page.setCurrent(queryRuleRequest.getPageNo());
//        page.setSize(queryRuleRequest.getPageSize());
//        LambdaQueryWrapper<Goods> lambdaQueryWrapper = Wrappers.<Goods>lambdaQuery()
//                .eq(!StringUtils.isEmpty(queryRuleRequest.getCommodityCode()), Goods::getCommodityCode, queryRuleRequest.getCommodityCode());
////                .like(!StringUtils.isEmpty(queryRuleRequest.getGoodsName()), Goods::getCommodityName, queryRuleRequest.getGoodsName()
//
//        if (!StringUtils.isEmpty(queryRuleRequest.getGoodsName())) {
//            lambdaQueryWrapper.and(r -> r.like(Goods::getCommodityName, queryRuleRequest.getGoodsName()).or().like(Goods::getCommodityCode, queryRuleRequest.getGoodsName()));
//        }
//
//        if (!StringUtils.isEmpty(queryRuleRequest.getPym())) {
//            lambdaQueryWrapper.and(r -> r.like(Goods::getCommodityName, queryRuleRequest.getPym())
//                    .or()
//                    .like(Goods::getCommodityCode, queryRuleRequest.getPym())
//                    .or()
//                    .like(Goods::getProPym, queryRuleRequest.getPym())
//            );
//        }
//
//
//        Page<Goods> page1 = goodsService.page(page, lambdaQueryWrapper);
//        return new SuccessResponseData<>(PageResultFactory.createPageResult(page1));
//    }


}
